//
//  SJShareBar.m
//  WSL
//
//  Created by CJ on 2018/1/26.
//  Copyright © 2018年 sanjuetech. All rights reserved.
//

#import "SJShareBar.h"
#import "SJBlockItem.h"


@interface SJShareBar ()

@property (strong, nonatomic) UIView *containTopView;
@property (strong, nonatomic) UIButton *buttonItem_0;
@property (strong, nonatomic) UIButton *buttonItem_1;
@property (strong, nonatomic) UIButton *buttonItem_2;
@property (strong, nonatomic) UIButton *buttonItem_3;
@property (strong, nonatomic) UIView *containBottomView;
@property (strong, nonatomic) UIButton *buttonItem_4;
@property (strong, nonatomic) UIButton *buttonItem_5;
@property (strong, nonatomic) UIButton *buttonItem_6;
@property (strong, nonatomic) UIButton *buttonItem_7;


@property (strong, nonatomic) UIButton *buttonItem_8; // 眼睛

@end


@implementation SJShareBar

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    // avatar
    
    [self addSubview:self.buttonItem_0];
    self.buttonItem_0.sd_layout
    .topSpaceToView(self, 0)
    .leftSpaceToView(self, SJLayoutFrame(20))
    .heightIs(SJLayoutFrame(60))
    .widthIs(SJLayoutFrame(60));
    
    // top
    [self addSubview:self.containTopView];
    self.containTopView.sd_layout
    .centerYEqualToView(self)
    .centerXEqualToView(self)
    .heightIs(SJLayoutFrame(60))
    .widthIs(SJScreenW - SJLayoutFrame(40));
    
    [self.containTopView addSubview:self.buttonItem_1];
    self.buttonItem_1.sd_layout.heightIs(SJLayoutFrame(60));
    
    [self.containTopView addSubview:self.buttonItem_2];
    self.buttonItem_2.sd_layout.heightIs(SJLayoutFrame(60));
    
    [self.containTopView addSubview:self.buttonItem_3];
    self.buttonItem_3.sd_layout.heightIs(SJLayoutFrame(60));
    
    [self.containTopView addSubview:self.buttonItem_8];
    self.buttonItem_8.sd_layout.heightIs(SJLayoutFrame(60));
    
    NSMutableArray *tempTop = [NSMutableArray new];
    [tempTop addObject:self.buttonItem_1];
    [tempTop addObject:self.buttonItem_2];
    [tempTop addObject:self.buttonItem_8];
    [tempTop addObject:self.buttonItem_3];
    
    [self.containTopView setupAutoMarginFlowItems:[tempTop copy] withPerRowItemsCount:tempTop.count itemWidth:SJLayoutFrame(60) verticalMargin:0 verticalEdgeInset:0 horizontalEdgeInset:0];
    
    
    // bottom
    [self addSubview:self.containBottomView];
    self.containBottomView.sd_layout
    .bottomSpaceToView(self, 0)
    .centerXEqualToView(self)
    .heightIs(SJLayoutFrame(60))
    .widthIs(SJScreenW - SJLayoutFrame(40));
    
    [self.containBottomView addSubview:self.buttonItem_4];
    self.buttonItem_4.sd_layout.heightIs(SJLayoutFrame(60));
    
    [self.containBottomView addSubview:self.buttonItem_5];
    self.buttonItem_5.sd_layout.heightIs(SJLayoutFrame(60));
    
    [self.containBottomView addSubview:self.buttonItem_6];
    self.buttonItem_6.sd_layout.heightIs(SJLayoutFrame(60));
    
    [self.containBottomView addSubview:self.buttonItem_7];
    self.buttonItem_7.sd_layout.heightIs(SJLayoutFrame(60));
    
    NSMutableArray *tempBottom = [NSMutableArray new];
    [tempBottom addObject:self.buttonItem_4];
    [tempBottom addObject:self.buttonItem_5];
    [tempBottom addObject:self.buttonItem_6];
    [tempBottom addObject:self.buttonItem_7];
    
    [self.containBottomView setupAutoMarginFlowItems:[tempBottom copy] withPerRowItemsCount:tempBottom.count itemWidth:SJLayoutFrame(60) verticalMargin:0 verticalEdgeInset:0 horizontalEdgeInset:0];
    
    
    // 分享到
    UILabel *shareLabel = [UILabel new];
    shareLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(13)];
    shareLabel.textAlignment = NSTextAlignmentLeft;
    shareLabel.text = @"分享到:";
    shareLabel.textColor = SJColorHexString(@"#999999");
    
    [self addSubview:shareLabel];
    shareLabel.sd_layout
    .bottomSpaceToView(self.containBottomView, SJLayoutFrame(10))
    .leftSpaceToView(self, SJLayoutFrame(30))
    .heightIs(SJLayoutFrame(15))
    .widthIs(SJScreenW / 2);
    
}

- (void)setFrame:(CGRect)frame
{
    frame.size = CGSizeMake(SJScreenW, SJLayoutFrame(264));
    [super setFrame:frame];
}


- (void)setItem:(SJBlockItem *)item {
    
    _item = item;
    
    if ([self isMe]) { // 自己
        
        self.buttonItem_3.hidden = YES;
        
        if ([item.is_published isEqualToString:@"2"]) {  // 已发布
            [self.buttonItem_1 setImage:[UIImage imageNamed:@"01_more_released"] forState:UIControlStateNormal];
        } else {
            [self.buttonItem_1 setImage:[UIImage imageNamed:@"01_more_release"] forState:UIControlStateNormal];
        }
        
    } else {  // 不是自己
        
        self.buttonItem_3.hidden = NO;
        
        if ([item.is_subscribe isEqualToString:@"2"]) {  // 已订阅
            [self.buttonItem_1 setImage:[UIImage imageNamed:@"01_more_collected"] forState:UIControlStateNormal];
        } else {
            [self.buttonItem_1 setImage:[UIImage imageNamed:@"01_more_collection"] forState:UIControlStateNormal];
        }
    }
    
    [self.buttonItem_0 setImageWithURL:[NSURL URLWithString:item.user_info.avatar] forState:UIControlStateNormal placeholder:[UIImage imageNamed:@"common_icon_defaultavatar"]];
    
}

- (void)setIsDisplay:(BOOL)isDisplay {
    
    
    self.buttonItem_8.selected = isDisplay;
}

- (BOOL)isMe {
    
    // 判断是不是自己
    SJUserInfoItem *myInfo = [SJUserInfoItem modelWithJSON:[SJUserDefaults objectForKey:SJUserInfo]];
    
    return [myInfo.ID isEqualToString:self.item.user_info.ID];
}


#pragma mark -- getter

- (UIView *)containTopView{
    
    if (!_containTopView) {
        
        _containTopView = [UIView new];
    }
    return _containTopView;
}

- (UIView *)containBottomView{
    
    if (!_containBottomView) {
        
        _containBottomView = [UIView new];
    }
    return _containBottomView;
}

- (void)selectedItem:(UIButton *)button {
    
    NSInteger index = button.tag;
    
    if ([self.delegate respondsToSelector:@selector(shareBar:didSelectItemAtIndex:button:)]) {
        
        [(id<SJShareBarDelegate>)self.delegate shareBar:self didSelectItemAtIndex:index button:button];
    }
}

- (UIButton *)buttonItem_0 {
    
    if (!_buttonItem_0) {
        
        _buttonItem_0 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_0 setImage:[UIImage imageNamed:@"common_icon_defaultavatar"] forState:UIControlStateNormal];
        [_buttonItem_0 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_0.sd_cornerRadiusFromHeightRatio = @(0.5);
        _buttonItem_0.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色
        _buttonItem_0.layer.borderWidth = SJLayoutFrame(2);//边框宽度
        _buttonItem_0.tag = 0;
        
    }
    return _buttonItem_0;
}

- (UIButton *)buttonItem_1 {
    
    if (!_buttonItem_1) {
        
        _buttonItem_1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_1 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_1.tag = 1;
        
    }
    return _buttonItem_1;
}

- (UIButton *)buttonItem_2 {
    
    if (!_buttonItem_2) {
        
        _buttonItem_2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_2 setImage:[UIImage imageNamed:@"01_more_search"] forState:UIControlStateNormal];
        [_buttonItem_2 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_2.tag = 2;
        
    }
    return _buttonItem_2;
}

- (UIButton *)buttonItem_3 {
    
    if (!_buttonItem_3) {
        
        _buttonItem_3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_3 setImage:[UIImage imageNamed:@"01_more_report"] forState:UIControlStateNormal];
        [_buttonItem_3 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_3.tag = 3;
        
    }
    return _buttonItem_3;
}

- (UIButton *)buttonItem_4 {
    
    if (!_buttonItem_4) {
        
        _buttonItem_4 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_4 setImage:[UIImage imageNamed:@"01_more_wechat"] forState:UIControlStateNormal];
        [_buttonItem_4 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_4.tag = 4;
        
    }
    return _buttonItem_4;
}

- (UIButton *)buttonItem_5 {
    
    if (!_buttonItem_5) {
        
        _buttonItem_5 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_5 setImage:[UIImage imageNamed:@"01_more_circle"] forState:UIControlStateNormal];
        [_buttonItem_5 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_5.tag = 5;
        
    }
    return _buttonItem_5;
}

- (UIButton *)buttonItem_6 {
    
    if (!_buttonItem_6) {
        
        _buttonItem_6 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_6 setImage:[UIImage imageNamed:@"01_more_qq"] forState:UIControlStateNormal];
        [_buttonItem_6 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_6.tag = 6;
        
    }
    return _buttonItem_6;
}

- (UIButton *)buttonItem_7 {
    
    if (!_buttonItem_7) {
        
        _buttonItem_7 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_7 setImage:[UIImage imageNamed:@"01_more_blog"] forState:UIControlStateNormal];
        [_buttonItem_7 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_7.tag = 7;
        
    }
    return _buttonItem_7;
}

- (UIButton *)buttonItem_8 {
    
    if (!_buttonItem_8) {
        
        _buttonItem_8 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonItem_8 setImage:[UIImage imageNamed:@"01_icon_head_nor"] forState:UIControlStateNormal];
        [_buttonItem_8 setImage:[UIImage imageNamed:@"01_icon_head_sel"] forState:UIControlStateSelected];
        [_buttonItem_8 addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        _buttonItem_8.tag = 8;
        
    }
    return _buttonItem_8;
}

@end
